<?php
/*
 * @Author: 罗永生
 * @Date: 2021-11-22 20:01:39
 * @LastEditors: 罗永生
 * @LastEditTime: 2021-11-22 20:12:47
 * @Description:
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Builder;
use App\Scopes\TestScope;

class UserModel extends Model
{
    // use SoftDeletes;
    
    protected $table = 'users';
    protected $primaryKey = 'id';

    // protected $fillable = ['name'];
    // protected $dates = ['deleted_at'];

    public $timestamps = true;

    // const CREATED_AT = 'created_at';
    // const UPDATED_AT = 'updated_at';

    /**
     * 模型的“启动”方法.
     *
     * @return void
     */
    // protected static function boot()
    // {
    //     parent::boot();

    //     static::addGlobalScope(new TestScope);
    // }

    /**
     * 获取关联到用户的爱好
     */
    public function role()
    {
        // return $this->hasOne('App\Models\HobbyModel', 'user_id');
        // return $this->belongsTo('App\Models\HobbyModel', 'id', 'user_id');
        return $this->belongsTo('App\Models\RoleModel', 'role_id');
    }

    /**
     * 获取关联到用户的爱好
     */
    public function hobbys()
    {
        return $this->hasMany('App\Models\HobbyModel', 'user_id');
        // return $this->belongsTo('App\Models\HobbyModel', 'id');
    }

    /**
     * 获取关联到用户的爱好
     */
    public function hobbyCount()
    {
        return $this->hasMany('App\Models\HobbyModel', 'user_id')->where('status', 1);
        // return $this->belongsTo('App\Models\HobbyModel', 'id');
    }

    /**
     * 用户角色
     */
    public function roles()
    {
        // return $this->belongsToMany('App\Models\RoleModel', 'user_roles', 'user_id', 'role_id')->withPivot('aaa', 'created_at');
        return $this->belongsToMany('App\Models\RoleModel', 'user_roles', 'user_id', 'role_id')->wherePivot('status', 1);
    }
}
